<?php

class MemberModel extends Model
{
    //修改/添加数据
    public function set($id, $data)
    {
        if ($id) {
            $this->update($data, 'id=' . $id);
            return $id;
        }
        $this->insert($data);
        if ($this->get_insert_id()) return $this->get_insert_id();
        return false;
    }

    //删除数据
    public function del($id)
    {
        $this->delete('id=' . $id);
    }

    //修改字段
    public function updatehits($id)
    {
        if ($id) {
            $sql = "update " . $this->prefix . "member set hits=hits+1 where id=$id";
            $this->db->query($sql);
        }
    }

    //查询记录中一个或多个字段
    public function select_listname($id, $name = false, $where)
    {
        if ($id) {
            $field = $name ? $name : '*';
            $sql = "select " . $field . " from " . $this->prefix . "member where $where";
            return $this->db->query($sql)->row_array();
        }
    }

    //前台列表
    public function get_all_list($where, $order, $limit, $ifcount = false)
    {
        if ($ifcount) {
            $sql = "select count(id) as total from " . $this->db->prefix . "member as m
		    left join " . $this->db->prefix . "member as g on m.typeid = g.id 
		    where $where";
            $res = $this->db->query($sql)->row_array();
            return $res['total'];
        }
        $sql = "select m.*,g.name from " . $this->db->prefix . "member as m
		left join " . $this->db->prefix . "member as g on m.typeid = g.id
		where $where order by $order limit $limit";
        $query = $this->db->query($sql)->result_array();
        return $query;
    }

    //前台详情
    public function get_all_show($id)
    {
        if ($id) {
            $sql = "select * from " . $this->prefix . "member
				 where id=$id";
            return $this->db->query($sql)->row_array();
        }
    }

    //后台列表
    public function get_admin_list($where, $order, $limit, $ifcount = false)
    {
        if ($ifcount) {
            $sql = "select count(id) as total from " . $this->prefix . "member
					where $where";
            $res = $this->db->query($sql)->row_array();
            return $res['total'];
        }
        $sql = "select * from " . $this->prefix . "member
		        where $where order by $order limit $limit";
        $res = $this->db->query($sql)->result_array();
        return $res;
    }

    //后台详情
    public function get_admin_show($id)
    {
        if ($id) {
            $sql = "select * from " . $this->prefix . "member
				 where id=$id";
            return $this->db->query($sql)->row_array();
        }
    }

    public function get_one_openid($openid){
        $sql = "select count(id) as total from " . $this->prefix . "member
				 where status=666 and is_daoqi = 2 and openid = '$openid'";
        return $this->db->query($sql)->row_array();
    }

    public function get_all_daoqi($where)
    {
        $sql = "select * from " . $this->prefix . "member
		        where $where";
        $res = $this->db->query($sql)->result_array();
        return $res;
    }

}